/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmUpdate.java
 *
 * Created on Jul 29, 2009, 11:58:49 PM
 */
package cconverter;

import javax.swing.JFrame;
import org.jdesktop.application.Action;

/**
 *
 * @author SapunBoj
 */
public class FrmUpdate extends javax.swing.JFrame {

    JFrame _frm = this;
    FrmInfo _frmInfo = new FrmInfo(this, true);
    Maps maps = new Maps();
    Pxml pxml = new Pxml();

    /** Creates new form FrmUpdate */
    public FrmUpdate() {
        initComponents();
        myInit();
        _frm.pack();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblLastUpdate = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        lblProgres = new javax.swing.JLabel();
        pbProgress = new javax.swing.JProgressBar();
        btnUpdate = new javax.swing.JButton();
        lblStatus = new javax.swing.JLabel();
        btnClose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cconverter.CConverterApp.class).getContext().getResourceMap(FrmUpdate.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        lblLastUpdate.setText(resourceMap.getString("lblLastUpdate.text")); // NOI18N
        lblLastUpdate.setName("lblLastUpdate"); // NOI18N
        lblLastUpdate.setPreferredSize(new java.awt.Dimension(96, 11));

        jSeparator1.setName("jSeparator1"); // NOI18N

        lblProgres.setText(resourceMap.getString("lblProgres.text")); // NOI18N
        lblProgres.setName("lblProgres"); // NOI18N

        pbProgress.setMaximum(33);
        pbProgress.setName("pbProgress"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(cconverter.CConverterApp.class).getContext().getActionMap(FrmUpdate.class, this);
        btnUpdate.setAction(actionMap.get("runUpdate")); // NOI18N
        btnUpdate.setIcon(resourceMap.getIcon("btnUpdate.icon")); // NOI18N
        btnUpdate.setText(resourceMap.getString("btnUpdate.text")); // NOI18N
        btnUpdate.setName("btnUpdate"); // NOI18N

        lblStatus.setText(resourceMap.getString("lblStatus.text")); // NOI18N
        lblStatus.setName("lblStatus"); // NOI18N

        btnClose.setAction(actionMap.get("clickClose")); // NOI18N
        btnClose.setText(resourceMap.getString("btnClose.text")); // NOI18N
        btnClose.setName("btnClose"); // NOI18N
        btnClose.setPreferredSize(new java.awt.Dimension(61, 25));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblProgres)
                .addContainerGap(313, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pbProgress, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblStatus)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 256, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addComponent(btnUpdate)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblLastUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblLastUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblProgres)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pbProgress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnUpdate)
                    .addComponent(lblStatus)
                    .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public void myInit() {
        // additional settings
        _frm.setResizable(false);
        _frm.setIconImage(Sets.icon);
        _frm.setLocationRelativeTo(this._frm);
        if (pxml.checkFile()) {
            try {
                lblLastUpdate.setText("Last update : " + pxml.getLastUpdate());
            } catch (Exception ex) {
                new FrmInfo(this, true).setInfo("Error", ex.getLocalizedMessage());
            }
        } else {
            // nothing just chiling killing
        }
    }

    @Action
    public void clickClose() {
        _frm.setVisible(false);
        _frm.dispose();
    }

    @Action
    public void runUpdate() {
        class wThread extends Thread {

            @Override
            public void run() {
                try {
                    pxml.makeFile();
                    maps.setLastUpdate(pxml.getLastUpdate());
                } catch (Exception ex) {
                    _frmInfo.setInfo("Connection error", "Connection error : " + ex.getLocalizedMessage());
                    _frmInfo.setVisible(true);
                }
                try {
                    maps.fillCMap();
                    maps.fillVMap();
                    maps.setLastUpdate(pxml.getLastUpdate());
                } catch (Exception ex) {
                    _frmInfo.setInfo("Error", "Error: " + ex.getLocalizedMessage());
                }
                lblStatus.setText("Currency list updated");
                try {
                    lblLastUpdate.setText("Last update : " + new Pxml().getLastUpdate());
                } catch (Exception ex) {
                    new FrmInfo(_frm, true).setInfo("Error", "System error: " + ex.getLocalizedMessage());
                }
                pbProgress.setIndeterminate(false);
                pbProgress.setValue(pbProgress.getMaximum());
                this.interrupt();
            }// run
        }// wThread
        pbProgress.setIndeterminate(true);
        Thread _th = new wThread();
        _th.start();

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblLastUpdate;
    private javax.swing.JLabel lblProgres;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JProgressBar pbProgress;
    // End of variables declaration//GEN-END:variables
}
